
package com.wuye.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Repository;

import com.wuye.common.dao.hibernate.BaseDaoHibernate;
import com.wuye.common.util.string.StrUtil;
import com.wuye.common.vo.CommonObjectVO;
import com.wuye.common.vo.PageInfo;
import com.wuye.common.vo.ParkingSearchVO;
import com.wuye.common.vo.ParkingUpdateInfo;
import com.wuye.common.vo.StatVO;
import com.wuye.constants.BaseConstants;
import com.wuye.dao.ParkingDao;
import com.wuye.entity.Parking;
import com.wuye.mapper.CommonMapper;
import com.wuye.util.MyBatisSessionFactory;
@Repository(value="parkingDao")
public class ParkingDaoImpl extends BaseDaoHibernate implements ParkingDao
{

	public PageInfo getParkingType(Map<String, Object> map, int currentPage,
			int perPageNum) {
		if (map == null){
			return null;
		}
		StringBuffer hql = new StringBuffer();
		List<Object> params = new ArrayList<Object>();
		hql.append("from ParkingType c where 1=1 ")
		.append(" and c.statusCd = ? ");
		params.add(BaseConstants.STATUS_VALID);
		String fuzzy = (String) map.get("fuzzy");
		String parkingTypeName = (String) map.get("parkingTypeName");
		Integer parkingTypeId = (Integer)map.get("parkingTypeId");
		
		Integer communityId = (Integer)map.get("communityId");
		if (communityId != null && !communityId.equals(0)){
			hql.append(" and c.communityId = ? ");
			params.add(communityId);
		}
		if (!StrUtil.isNullOrEmpty(parkingTypeName)){
			if ("true".equals(fuzzy)){
				hql.append(" and c.parkingTypeName like ? ");
				params.add("%"+parkingTypeName+"%");
			}else{
				hql.append(" and c.parkingTypeName = ? ");
				params.add(parkingTypeName);
			}
		}
		
		if (parkingTypeId != null && !parkingTypeId.equals(0)){
			hql.append(" and c.parkingTypeId = ? ");
			params.add(parkingTypeId);
		}
		
		PageInfo pageInfo = super.findPageInfoByHQLAndParams(hql.toString(), params, currentPage, perPageNum);
		return pageInfo;
	}

	public PageInfo getParking(Map<String, Object> map, int currentPage,
			int perPageNum) {
		if (map == null){
			return null;
		}
		StringBuffer hql = new StringBuffer();
		List<Object> params = new ArrayList<Object>();
		
		hql.append("from Parking c where 1=1 ")
		.append(" and c.statusCd = ? ");
		params.add(BaseConstants.STATUS_VALID);
		String fuzzy = (String) map.get("fuzzy");
		String parkingNbr = (String) map.get("parkingNbr");
		Integer parkingId = (Integer)map.get("parkingId");
		
		Integer communityId = (Integer)map.get("communityId");
		if (communityId != null && !communityId.equals(0)){
			hql.append(" and c.communityId = ? ");
			params.add(communityId);
		}
		if (!StrUtil.isNullOrEmpty(parkingNbr)){
				hql.append(" and c.parkingNbr = ? ");
				params.add(parkingNbr);
		}
		
		if (parkingId != null && !parkingId.equals(0)){
			hql.append(" and c.parkingId = ? ");
			params.add(parkingId);
		}
		
		PageInfo pageInfo = super.findPageInfoByHQLAndParams(hql.toString(), params, currentPage, perPageNum);
		return pageInfo;
	}

	public void addParking(Parking parking) throws Exception 
	{
		super.addObject(parking);		
	}

	public void updateParking(Parking parking) throws Exception 
	{
		super.updateObject(parking);		
	}

	public void delParking(int parkingId) throws Exception 
	{
		super.removeObject(Parking.class, parkingId);		
	}

	public Parking getParking(int parkingId) throws Exception 
	{
		return (Parking)super.getObject(Parking.class, parkingId);
	}
	
	public CommonObjectVO getParkingInfo(Map<String, Object> map) throws Exception
	{
		CommonObjectVO commonObjectVO = null;
		SqlSession session = null;
		
		try 
		{
			session = MyBatisSessionFactory.getSession();
			CommonMapper commonMapper = session.getMapper(CommonMapper.class);
			commonObjectVO = commonMapper.getParkingInfo(map);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			session.close();
		}
		
		return commonObjectVO;
		
	}

	public List<ParkingSearchVO> getParkingSearchInfo(Map<String, Object> map) 
	{
		List<ParkingSearchVO> parkingSearchVOs = null;
		SqlSession session = null;
		
		try 
		{
			session = MyBatisSessionFactory.getSession();
			CommonMapper commonMapper = session.getMapper(CommonMapper.class);
			parkingSearchVOs = commonMapper.getParkingSearchInfo(map);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			session.close();
		}
		
		return parkingSearchVOs;
	}

	public StatVO getParkingSearchStats(Map<String, Object> map) 
	{
		StatVO statVO = null;
		SqlSession session = null;
		
		try 
		{
			session = MyBatisSessionFactory.getSession();
			CommonMapper commonMapper = session.getMapper(CommonMapper.class);
			statVO = commonMapper.getParkingSearchStats(map);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			session.close();
		}
		
		return statVO;
	}

	public ParkingUpdateInfo getParkingUpdateInfo(int parkingId) 
	{
		ParkingUpdateInfo updateInfo = null;
		SqlSession session = null;
		
		try 
		{
			session = MyBatisSessionFactory.getSession();
			CommonMapper commonMapper = session.getMapper(CommonMapper.class);
			updateInfo = commonMapper.getParkingUpdateInfo(parkingId);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			session.close();
		}
		
		return updateInfo;
	}

	public PageInfo getBuildingType(Map<String, Object> map, int currentPage,
			int perPageNum) {
		// TODO Auto-generated method stub
		return null;
	}



}
